using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Cou;
using net_work.Data.Enum.Cou;

namespace net_work.Config.DataBaseConfig.Cou;

/// <summary>
/// 教学实体配置类
/// </summary>
public class TeachingEntityTypeConfiguration : IEntityTypeConfiguration<Teaching>
{
    public void Configure(EntityTypeBuilder<Teaching> builder)
    {
        builder.ToTable("cou_teach");

        builder.HasKey(t => t.Id);
        builder.HasIndex(t => t.TeachCode).IsUnique();

        // 配置字段名映射
        builder.Property(t => t.Id).HasColumnName("teach_id");
        builder.Property(t => t.TeachGrade).HasColumnName("teach_grade").IsRequired();
        builder.Property(t => t.TeachCode).HasColumnName("teach_code").IsRequired().HasMaxLength(64);
        builder.Property(t => t.TeachCredits).HasColumnName("teach_cre").IsRequired().HasColumnType("decimal(4,1)");
        builder.Property(t => t.TeachCapacity).HasColumnName("teach_cap").IsRequired();
        builder.Property(t => t.TeachWeekLength).HasColumnName("teach_week_len").IsRequired();
        builder.Property(t => t.TeachTheoryHours).HasColumnName("teach_the_hours").IsRequired();
        builder.Property(t => t.TeachPracticalHours).HasColumnName("teach_prac_hours").IsRequired();
        builder.Property(t => t.TeachComposition).HasColumnName("teach_cpn").HasMaxLength(64);
        builder.Property(t => t.TeachLocation).HasColumnName("teach_loc").HasMaxLength(64);

        // 将枚举映射为字符串存储
        builder.Property(t => t.TeachStatus)
            .HasColumnName("teach_status")
            .IsRequired()
            .HasMaxLength(64)
            .HasDefaultValue(TeachStatus.UnRelease) // 默认值
            .HasConversion(
                v => v.ToString(), // 枚举转字符串
                v => (TeachStatus)Enum.Parse(typeof(TeachStatus), v) // 字符串转枚举
            );

        builder.Property(t => t.CourseId).HasColumnName("cou_id").IsRequired();

        builder.Property(t => t.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(t => t.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(t => t.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(t => t.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(t => t.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(t => t.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}